﻿#include "mywidget.h"

#include <qpainter.h>
#include <QTime>
#include <QDebug>
#include <QMouseEvent>
#include <QPushButton>
#include <QSlider>
#include <QRadioButton>

const int OFFSET = 20;
const int TOOLSSIZE = 0;

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
    resize_array(3,3);
    sc();

    QSlider *pSlider1 = new QSlider(this);
    pSlider1->setGeometry(30,10,10,400);
    pSlider1->setOrientation(Qt::Vertical);  // 水平方向
    pSlider1->setMinimum(3);        // 最小值
    pSlider1->setMaximum(100);      // 最大值
    pSlider1->setSingleStep(1);     // 步长

    QSlider *pSlider2 = new QSlider(this);
    pSlider2->setGeometry(50,10,10,400);
    pSlider2->setOrientation(Qt::Vertical);  // 水平方向
    pSlider2->setMinimum(3);        // 最小值
    pSlider2->setMaximum(100);      // 最大值
    pSlider2->setSingleStep(1);     // 步长

    QPushButton *pBtn = new QPushButton(this);
    pBtn->setGeometry(80,50,40,200);
    pBtn->setText("更新");

//    QRadioButton *rbutton1 = new QRadioButton("四连通", this);
//    rbutton1->setGeometry(50,520,100,20);

//    QRadioButton *rbutton2 = new QRadioButton("八连通", this);
//    rbutton2->setGeometry(50,580,100,20);

    connect(pSlider1, SIGNAL(valueChanged(int)), this, SLOT(set_m_Value(int)));
    connect(pSlider2, SIGNAL(valueChanged(int)), this, SLOT(set_n_Value(int)));
    connect(pBtn,SIGNAL(clicked()),this,SLOT(gx()));
}

void MyWidget::paintEvent(QPaintEvent *event)
{
    int w = this->width();
    int h = this->height();
    int b1 = (w - OFFSET*2-TOOLSSIZE)/_n;
    int b2 = (h - OFFSET*2)/_m;
    int b = b1;
    if (b1>b2) b = b2;
    QPainter painter(this);
    int ww = (w - _n*b)/2+TOOLSSIZE;
    int hh = (h - _m*b)/2;
    for (int i=0;i<_n;i++)
    {
        for(int j=0;j<_m;j++)
        {
            QBrush brush;

            if (_array[j][i] == -1)
                brush.setColor(Qt::black);//设置黑色
            else
                brush.setColor(Qt::white);//设置白色

            brush.setStyle(Qt::SolidPattern);//设置填充的风格，可以使用帮助文档查看有哪些风格
            painter.setBrush(brush);
            painter.setPen(Qt::green);
            QRect rect((ww+i*b),(hh+j*b),b,b);
            painter.drawRect(rect);
        }
    }

    QFont font;
    font.setFamily("Microsoft YaHei");
    font.setPointSize(40);

    painter.setPen(Qt::red);
    QRect rect(40,420,180,80);
    painter.setFont(font);
    painter.drawText(rect, Qt::AlignCenter, QString("%1").arg(_count));

    return QWidget::paintEvent(event);
}

void MyWidget::_work_a_grid()
{   
    while (!_q1.empty())
    {
        int j = _q1.dequeue();
        int i = _q2.dequeue();

        if ((j-1)>=0 && _array[j-1][i] == 1)
        {
            _q1.enqueue(j-1);
            _q2.enqueue(i);
            _array[j-1][i] = -1;
        }

        if ((i+1)<_n && _array[j][i+1] == 1)
        {
            _q1.enqueue(j);
            _q2.enqueue(i+1);
            _array[j][i+1] = -1;
        }

        if ((j+1)<_m && _array[j+1][i] == 1)
        {
            _q1.enqueue(j+1);
            _q2.enqueue(i);
            _array[j+1][i] = -1;
        }

        if ((i-1)>=0 && _array[j][i-1] == 1)
        {
            _q1.enqueue(j);
            _q2.enqueue(i-1);
            _array[j][i-1] = -1;
        }

         _work_a_grid();
    }
}

void MyWidget::sc()
{
    QString fileName = "data.txt";
    QFile datafile(fileName);
    if(datafile.open(QIODevice::WriteOnly|QIODevice::Text))
    {
        QTextStream streamFile(&datafile);
        streamFile<< QString("%1 %2").arg(_m).arg(_n)<<"\n";
        for(int i=0;i<_m;i++)
        {
            QString s;
            for(int j=0;j<_n;j++)
            {
                int c = _array[i][j];
                if (c == 1 || c == -1) c = 1;
                if (j==0)
                    s.append(QString("%1").arg(c));
                else
                    s.append(QString(" %1").arg(c));
            }
            if (i==0)
                streamFile<<s;
            else
                 streamFile<<"\n"<<s;
        }
        datafile.close();
    }

    fileName = "out.txt";
    QFile outfile(fileName);
    if(outfile.open(QIODevice::WriteOnly|QIODevice::Text))
    {
        QTextStream streamFile(&outfile);
        streamFile<< QString("%1").arg(_count)<<"\n";
        outfile.close();
    }

}

void MyWidget::gx()
{
    resize_array(_m,_n);
    sc();
}

void MyWidget::resize_array(int m,int n)
{
    //qDebug("x");
    _m = m;
    _n = n;
    _array.resize(_m);
    QTime time = QTime::currentTime();
    qsrand(time.msec()+time.second()*1000);
    for(int j=0;j<_array.size();j++)
    {
        _array[j].resize(_n);
        for (int i = 0;i<_array[j].size();i++)
        {
            int nn = qrand() % 3;
            if (nn <2)
                _array[j][i] = 0;
            else
                _array[j][i] = 1;
        }
    }
    _cac();
    // qDebug("连通块:%d",_count);
    update();
}

void MyWidget::set_m_Value(int v)
{
    resize_array(v,_n);
    sc();
}

void MyWidget::set_n_Value(int v)
{
    resize_array(_m,v);
    sc();
}

void MyWidget::_cac()
{
    _count = 0;
    for(int j=0;j<_array.size();j++)
    {
        for (int i = 0;i<_array[j].size();i++)
        {
            _q1.clear();
            _q2.clear();
            if (_array[j][i] <= 0) continue;
            _count++;
            _q1.enqueue(j);
            _q2.enqueue(i);
            _array[j][i] = -1;
            _work_a_grid();
        }
    }
}



